import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.RspInterface 1.0
import QtCharts 2.2
//import com.ComboData 1.0
import "public.js" as Public


//本代理风险分布图
Item {
    property var tabName
    property var branch
    property var showDate
    property var sectionSpace: 10
    property var kvSpace: 2
    //    property ComboData branchModel: g_MainQuo.getMyBranchs()
    property var condPer: 0

    ColumnLayout {
        anchors.fill: parent
        RowLayout {
            anchors.top: parent.top
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter
            z: 5

            Text {
                id: beginT
                text: "开始日期" + colon
                Layout.alignment: Qt.AlignHCenter
                //                anchors.left: cmbBranch.right
                //                anchors.leftMargin: sectionSpace
            }
            DatePicker {
                id: b_date
                Layout.preferredWidth: 110
                anchors.left: beginT.right
                anchors.leftMargin: kvSpace
            }

            Text {
                id: endT
                text: "结束日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: b_date.right
                anchors.leftMargin: sectionSpace
            }
            DatePicker {
                id: e_date
                Layout.preferredWidth: 110
                anchors.left: endT.right
                anchors.leftMargin: kvSpace
            }
            //        }
            //        RowLayout {
            //            anchors.top: parent.top
            //            anchors.topMargin: 5
            //            anchors.left: parent.left
            //            anchors.leftMargin: 20
            //            anchors.right: parent.right
            //            //            anchors.rightMargin: 20
            //            Layout.preferredHeight: 30
            //            Layout.alignment: Qt.AlignHCenter
            CheckBox {
                id: check1
                text: "正常"
                anchors.left: e_date.right
                anchors.leftMargin: sectionSpace * 2
            }
            CheckBox {
                id: check2
                text: "追保"
                anchors.left: check1.right
                anchors.leftMargin: sectionSpace
            }
            CheckBox {
                id: check3
                text: "相对强平"
                anchors.left: check2.right
                anchors.leftMargin: sectionSpace
            }
            CheckBox {
                id: check4
                text: "绝对强平"
                anchors.left: check3.right
                anchors.leftMargin: sectionSpace
            }

            Button {
                text: "查询"
                anchors.right: parent.right
                anchors.rightMargin: 20
                onClicked: {
                    var bDate = b_date.getDateStr()
                    var eDate = e_date.getDateStr()
                    if (parseInt(bDate) > parseInt(eDate)) {
                        tipInput.text = "开始日期不能大于结束日期!"
                        tipInput.open()
                        return
                    }
                    //af83f498130961011 lqh           B00000                          #end_time=20180820#begin_time=20180720#oper_flag=0#risk_level=11∧#
                    //af9fd649130961011 lqh           B00000                          #end_time=20180820#begin_time=20180720#oper_flag=0#risk_level=11∧21∧#
                    var req = "begin_time=" + b_date.getDateStr()
                    req += "#end_time=" + e_date.getDateStr()
                    var riskHead = "#risk_level="
                    var risk_level = riskHead
                    if (check1.checked)
                        risk_level += "11∧"
                    if (check2.checked)
                        risk_level += "21∧"
                    if (check3.checked)
                        risk_level += "31∧"
                    if (check4.checked)
                        risk_level += "41∧"

                    if (risk_level.length > riskHead.length) {
                        risk_level += "#"
                        req += risk_level
                    } else {
                        tipInput.text = "请选择风险等级!"
                        tipInput.open()
                        return
                    }
                    var rsp = g_MainQuo.queryAgentRiskCustCount(req)

                    custDistrModel.clear()
                    var almName = "branch_id"
                    var rowCount = rsp.getAlmSize(almName)
                    for (var i = 0; i < rowCount; ++i) {
                        custDistrModel.append({
                                                  Date: rsp.getAlmString(
                                                            almName, i, 0),
                                                  Branch: rsp.getAlmString(
                                                              almName, i, 1),
                                                  TotalCount: rsp.getAlmString(
                                                                  almName, i,
                                                                  2),
                                                  CondCount: rsp.getAlmString(
                                                                 almName, i, 3)
                                              })
                    }
                }
            }
        }

        SplitView {
            anchors.top: parent.top
            anchors.topMargin: 40
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 40
            //            Layout.fillHeight: true
            TableView {
                width: parent.width * 0.7
                Layout.minimumWidth: 250
                model: custDistrModel
                TableViewColumn {
                    title: "日期"
                    role: "Date"
                    resizable: true
                    width: parent.width * 0.1
                }

                TableViewColumn {
                    title: "代理机构"
                    role: "Branch"
                    resizable: true
                    width: parent.width * 0.1
                }

                TableViewColumn {
                    title: "总客户数"
                    role: "TotalCount"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "满足条件客户数"
                    role: "CondCount"
                    resizable: true
                    width: parent.width * 0.1
                }
                onDoubleClicked: {
                    showDate = custDistrModel.get(row).Date
                    branch = custDistrModel.get(row).Branch
                    var num = custDistrModel.get(row).CondCount
                    var total = custDistrModel.get(row).TotalCount
                    condPer = 0
                    if (total > 0)
                        condPer = Math.round(num / total * 10000) / 100.00
                }
            }

            ChartView {
                title: qsTr("代理商风险分布图(") + showDate + qsTr(")")
                Layout.minimumWidth: 50
                Layout.fillWidth: true
                theme: ChartView.ChartThemeBrownSand
                antialiasing: true
                visible: condPer > 0 ? true : false

                StackedBarSeries {
                    id: mySeries
                    axisX: BarCategoryAxis {
                        categories: ["2007", "2008", "2009", "2010", "2011", "2012"]
                    }
                    BarSet {
                        label: "Bob"
                        values: [2, 2, 3, 4, 5, 6]
                    }
                }
                //                PercentBarSeries {
                //                    name: "代理机构"

                //                    axisY: CategoryAxis {
                //                        min: 0
                //                        max: 1
                //                    }
                //                    PercentBarSeries {
                //                        axisX: BarCategoryAxis {
                //                            categories: ["branch"]
                //                        }
                //                        BarSet {
                //                            label: "Bob"
                //                            values: [2]
                //                        }
                //                    }

                //                }
            }
        }
    }

    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }

    ListModel {
        id: custDistrModel
    }
}
